package com.wenyu.wenyuyuedu.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.widget.RadioButton;
import android.widget.TextView;

import com.wenyu.wenyuyuedu.global.MyApplication;


/**
 *
 */
public class DrawbleUtil {

    private DrawbleUtil() {
    }

    /**
     * 修改RadioButton的drawble图片的尺寸
     * @param context
     * @param rb
     * @param Resource
     * @param dp
     */
    public static void updateDrawbleTop(Context context, RadioButton rb, int Resource, int dp) {
        Drawable drawable = MyApplication.mApp.getResources().getDrawable(Resource);
        int px_size = DensityUtil.dip2px(context,dp);
        drawable.setBounds(0,0,px_size,px_size);
        rb.setCompoundDrawables(null,drawable,null,null);
    }

    /**
     * 修改TextView的drawble图片的尺寸
     * @param context
     * @param rb
     * @param Resource
     * @param dp
     */
    public static void updateDrawbleTop(Context context, TextView rb, int Resource, int dp) {
        Drawable drawable = MyApplication.mApp.getResources().getDrawable(Resource);
        int px_size = DensityUtil.dip2px(context,dp);
        drawable.setBounds(0,0,px_size,px_size);
        rb.setCompoundDrawables(null,drawable,null,null);
    }

    /**
     * 修改Textview右侧drawble图片的尺寸
     * @param context
     * @param rb        控件
     * @param Resource  图片的位置
     * @param dp
     */
    public static void updateDrawbleRight(Context context, TextView rb, int Resource, int dp) {
        Drawable drawable = MyApplication.mApp.getResources().getDrawable(Resource);
        int px_size = DensityUtil.dip2px(context,dp);
        drawable.setBounds(0,0,px_size,px_size);
        rb.setCompoundDrawables(null,null,drawable,null);
    }

    /**
     *
     * @param context
     * @param rb        控件的位置
     * @param Resource  图片的位置
     * @param width
     * @param height
     */
    public static void updateDrawbleRight(Context context, TextView rb, int Resource, int width, int height) {
        Drawable drawable = MyApplication.mApp.getResources().getDrawable(Resource);
        int px_width = DensityUtil.dip2px(context,width);
        int px_height = DensityUtil.dip2px(context,height);
        drawable.setBounds(0,0,px_width,px_height);
        rb.setCompoundDrawables(null,null,drawable,null);
    }

    /**
     * 修改TextView左侧Drawble图片的尺寸
     * @param context
     * @param tv
     * @param Resource 图片位置
     * @param with_dp
     * @param height_dp
     */
    public static void updateDrawbleLeft(Context context, TextView tv, int Resource, int with_dp, int height_dp){
        Drawable drawable = MyApplication.mApp.getResources().getDrawable(Resource);
        int px_width= DensityUtil.dip2px(context,with_dp);
        int px_heiht = DensityUtil.dip2px(context,height_dp);
        drawable.setBounds(0,0,px_width,px_heiht);
        tv.setCompoundDrawables(drawable,null,null,null);
    }
    /**设置图片的尺寸*/
    public static Drawable settingImage(int imgId, int width, int height){
        Drawable drawable = MyApplication.mApp.getResources().getDrawable(imgId);
        drawable.setBounds(0,0,width,height);
        return drawable;
    }

    /**
     *  处理图片
     * @param bm 所要转换的bitmap
     * @param newWidth新的宽
     * @param newHeight新的高
     * @return 指定宽高的bitmap
     */
    public static Bitmap zoomImg(Bitmap bm, int newWidth , int newHeight){
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

    /**
     * 制作圆形图片
     * @param bitmap
     * @return
     */
    public Bitmap toRoundBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float roundPx;
        float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
        if (width <= height) {
            roundPx = width / 2;
            top = 0;
            bottom = width;
            left = 0;
            right = width;
            height = width;
            dst_left = 0;
            dst_top = 0;
            dst_right = width;
            dst_bottom = width;
        } else {
            roundPx = height / 2;
            float clip = (width - height) / 2;
            left = clip;
            right = width - clip;
            top = 0;
            bottom = height;
            width = height;
            dst_left = 0;
            dst_top = 0;
            dst_right = height;
            dst_bottom = height;
        }
        Bitmap output = Bitmap.createBitmap(width,
                height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
        final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
        final RectF rectF = new RectF(dst);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, src, dst, paint);
        return output;
    }
}
